package sharing

import base.Dimensions
import base.DimensionsType

//系统初始
class InitBaseService {
    def springSecurityService
//    def utilsService

    def init() {
        initDemensions()
    }


/*初始化 系统内置的为维度类型及维度*/
    def initDemensions(){
       def jcType= saveDimensionsType(code:"001",name:"基础数据归类" )
       def shijia= saveDimensions([code: "JC_001",name:"请假类别",dimensionsType:jcType])
        saveDimensions([code: "JC_001001",name:"事假（按缺勤扣减工资）",dimensionsType:jcType,parent:shijia])
        def bmsx= saveDimensions([code: "JC_002",name:"部门属性(核算用)",dimensionsType:jcType])
        saveDimensions([code: "JC_002001",name:"管理(核算用)",dimensionsType:jcType,parent:bmsx])
        saveDimensions([code: "JC_002002",name:"销售(核算用)",dimensionsType:jcType,parent:bmsx])
        saveDimensions([code: "JC_002003",name:"研发(核算用)",dimensionsType:jcType,parent:bmsx])
        saveDimensions([code: "JC_002004",name:"成本(核算用)",dimensionsType:jcType,parent:bmsx])


        def jc003= saveDimensions([code: "JC_003",name:"所属行业",dimensionsType:jcType])
        saveDimensions([code:"JC_003001",name:"IT/通信/电子/互联网",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003002",name:"金融业",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003003",name:"房地产/建筑业",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003004",name:"商业服务",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003005",name:"贸易/批发/零售/租赁业",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003006",name:"生产/加工/制造",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003007",name:"交通/运输/物流/仓储",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003008",name:"服务业",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003009",name:"文化/传媒/娱乐/体育",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003010",name:"能源/矿产/环保",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003011",name:"政府/非盈利机构",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003012",name:"农/林/牧/渔/其他",dimensionsType:jcType,parent:jc003])
        saveDimensions([code:"JC_003013",name:"跨领域经营",dimensionsType:jcType,parent:jc003])

        def jc004= saveDimensions([code: "JC_004",name:"客户性质",dimensionsType:jcType])
        saveDimensions([code: "JC_004001",name:"央企总部",dimensionsType:jcType,parent:jc004])
        saveDimensions([code: "JC_004002",name:"央企下属",dimensionsType:jcType,parent:jc004])
        saveDimensions([code: "JC_004003",name:"央企下属",dimensionsType:jcType,parent:jc004])
        saveDimensions([code: "JC_004004",name:"国企下属",dimensionsType:jcType,parent:jc004])
        saveDimensions([code: "JC_004005",name:"外企",dimensionsType:jcType,parent:jc004])
        saveDimensions([code: "JC_004006",name:"民营企业",dimensionsType:jcType,parent:jc004])

        def jc005= saveDimensions([code: "JC_005",name:"区域",dimensionsType:jcType])
        //  todo  初始化区域  省市县

        def ryzz= saveDimensionsType(code:"002",name:"人员职责" )
        saveDimensions([code: "RY_001",name:"普通员工",dimensionsType:ryzz])
        saveDimensions([code: "RY_002",name:"部门经理",dimensionsType:ryzz])
        saveDimensions([code: "RY_003",name:"总经理",dimensionsType:ryzz])
        saveDimensions([code: "RY_004",name:"项目经理",dimensionsType:ryzz])

        def gzlx= saveDimensionsType(code:"003",name:"工作类型" )
    }

    def saveDimensionsType(map) {
        def domain = DimensionsType.findByCode(map.code);
        if (!domain) {
            domain = new DimensionsType(map); domain.sysInit=true
            domain = saveComm(domain).domain;
        } else {
            if (map.forceUpdate) {
                domain.properties = map
                domain = saveComm(domain).domain;
            }
        }
        domain
    }

    def saveDimensions(map) {
        def domain = Dimensions.findByCode(map.code);
        if (!domain) {
            domain = new Dimensions(map); domain.sysInit=true
            domain = saveComm(domain).domain;
        } else {
            if (map.forceUpdate) {
                domain.properties = map
                domain = saveComm(domain).domain;
            }
        }
        domain
    }



    def saveComm(domain) {
        domain.clearErrors(); def result = [success: true, error: '', domain: null]; if (!domain.save(flush: true)) {
            domain.errors.each { println(it); result.error += it; }; result.success = false;
        }; result.domain = domain; result
    }

}
